• Jump To … +
    main.js separate.js single.js web-apg-api.js main.js web-conv-api.js ast.js csv.js dangling-else.js display.js flags.js float.js limits.js main.js multiline-mode.js recursive.js replace.js rules.js split.js testonly.js trace.js udt.js unicode.js web-email.js word-boundaries.js main.js phone-number.js web-main.js web-phone-number.js main.js phone-number.js setup.js translate.js xml.js branch-fail-grammar.js main.js parent-mode-grammar.js setup.js universal-mode-grammar.js colors-app.js colors-callbacks.js colors.js main.js more-app.js more-setup.js more.js ast-callbacks.js bad-input.js basic.js ini-file.js main.js parser-callbacks.js setup.js trace.js anbncn.js and.js c-comment.js compound.js main.js nested.js not.js setup.js boundaries-grammar.js boundaries.js comment-grammar.js comment.js main.js negative-grammar.js negative.js positive-grammar.js positive.js setup.js main.js odata-grammar.js run.js setup.js area-code.js lookaround.js main.js phone-number.js setup.js simple.js all-operators.js default.js fancy-number.js limited-lines.js main.js select-operators.js select-rules.js setup.js main.js minimal.js parent-u.js parent.js phone-number.js setup.js stats.js trace.js universal-u.js universal.js callbacks.js grammar.js main.js parser.js writeHtml.js LICENSE.md README.md index.md
  • setup.js

  • §
    /*  *************************************************************************************
     *   copyright: Copyright (c) 2021 Lowell D. Thomas, all rights reserved
     *     license: BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
     *   ********************************************************************************* */
  • §

    This module provides all of the set up common to all tests.

    • a universal callback function is provided to check each rule against a map of rule constraints
    • the parser object is constructed
    • the grammar object is constructed
    • a trace object is contructed for the trace test which will trace ALL rules and operators
    • a map of contraints - that is, some rules allow only a fixed set of strings to be matched
    • the universal callback function is assigned to every rule
    module.exports = function setup() {
      /* get all the "require"d modules */
      const fs = require('node:fs');
      const { apgLib } = require('apg-js');
      const GrammarCtor = require('./odata-grammar');
      const { parser: ParserCtor, ids, utils, trace: TraceCtor } = apgLib;
      const fileName = './src/odata/odata-abnf-testcases.json';
    
      /* the callback function to match constraints to rules */
      const universalCallback = function universalCallback(result, chars, phraseIndex, userData) {
        if (result.state === ids.MATCH) {
          userData.rulesTouched[result.ruleIndex] = true;
          const value = userData.map.get(result.ruleIndex);
          if (value) {
            const phrase = utils.charsToString(chars, phraseIndex, result.phraseLength);
            const found = value.find((element) => element === phrase);
            if (!found) {
              if (userData.trace) {
                console.log(
                  `=> universalCallback[${result.ruleIndex}]: phrase "${phrase}" not found in mapped list of phrases for this rule`
                );
              }
              result.state = ids.NOMATCH;
              result.phraseLength = 0;
            }
          }
        }
      };
      try {
        /* read the JSON tests and parse into a JSON object */
        const data = fs.readFileSync(fileName);
        const tests = JSON.parse(data.toString());
    
        /* construct the parser, grammar and trace objects */
        const odataParser = new ParserCtor();
        const odataGrammar = new GrammarCtor();
        const odataTrace = new TraceCtor();
    
        /* trace all rules and operators */
        odataTrace.filter.rules['<ALL>'] = true;
        odataTrace.filter.operators['<ALL>'] = true;
    
        /* set a call back for every rule */
        /* note that only callbacks for mapped rules are needed, but we are going to keep track of all rules that are touched */
        odataGrammar.rules.forEach((rule) => {
          odataParser.callbacks[rule.lower] = universalCallback;
        });
    
        /* create the map of constraints */
        const map = new Map();
        tests.constraints.forEach((c) => {
          const key = parseInt(c.ruleId, 10);
          const value = [];
          c.match.forEach((s) => {
            value.push(s);
          });
          map.set(key, value);
        });
    
        /* return the necessary setup info to the example functions */
        return { odataParser, odataTrace, odataGrammar, utils, tests, map };
      } catch (e) {
        console.log('setup error');
        console.log(e.message);
        return {};
      }
    };